package com.itfenghuang.controller;

import com.itfenghuang.bean.Student;
import com.itfenghuang.service.StudentServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddStudentServlet extends HttpServlet {
    private StudentServiceImp service=new StudentServiceImp();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.中文乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //2.获取请求参数值
        String sid = req.getParameter("sid");
        String sname = req.getParameter("sname");
        String sage = req.getParameter("sage");
        String ssex = req.getParameter("ssex");
        String smoney = req.getParameter("smoney");
        String cid = req.getParameter("cid");
        //调用添加的方法,将请求参数封装到学生对象中
        Student stu = new Student();
        stu.setSid(Integer.parseInt(sid));
        stu.setSname(sname);
        stu.setSage(Integer.parseInt(sage));
        stu.setSsex(ssex);
        stu.setSmoney(Integer.parseInt(smoney));
        stu.setCid(Integer.parseInt(cid));
        //调用添加方法
        service.addStudent(stu);
        //响应
        resp.setHeader("Refresh","3;URL=/stusys/index.jsp");
        resp.getWriter().write("添加成功,三秒后跳转到页面");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
    }
}
